const request = require('request')
const fs = require('fs');

checking()

function checking() {
  let bitmap = fs.readFileSync('./1.jpg');
  let base64str = Buffer.from(bitmap, 'binary').toString('base64'); // base64编码
  //console.log(base64str);

  var url ="https://aip.baidubce.com/rest/2.0/face/v3/detect?access_token=24.59c71e38d1e69977d54309f024730e98.2592000.1610546185.282335-23152618" 
  var formData = {
    image: base64str,
    image_type:"BASE64",
    face_field:"faceshape,facetype"
  }
  request.post({url:url, formData: formData},function (error, response, body) {
    if (error) {
      console.log(error)
    } else {
      console.log("body:" + body)
      var body = JSON.parse(body);
      if (body.error_msg == "SUCCESS") {
        face_token = body.result.face_list[0].face_token;
        console.log("face_token:" + body.result.face_list[0].face_token)
        match_verify(face_token)
      }
    }
  })
}

function match_verify(face_id) {
  var url2 ="https://aip.baidubce.com/rest/2.0/face/v3/match?access_token=24.59c71e38d1e69977d54309f024730e98.2592000.1610546185.282335-23152618"
  var formData = [
    {
      image: face_id,
      image_type:"FACE_TOKEN"
    },
    {
      //来自b.jpg 即基准图片
      image: "41d5e0a556e67424c4d375181264e542",
      image_type:"FACE_TOKEN"
    }
  ]

  /*
 application/json (JSON表单)
 */
  request({
    url: url2,
    method: "POST",
    json: true,
    headers: {
      "content-type": "application/json",
    },
    //body: JSON.stringify(formData)
    body: formData
  }, function(error, response, body) {
    console.log(error)
    console.log(response)
    console.log(body)
  })
}



